package com.wheel.autoconfigure.web.mvc.argumentresolver;/*
 * <h3>Title:wheel-parent系统</h3>
 * <p>Copyright (c) 2021, wnfbx. All rights reserved.</p>
 * @author wnfbx
 */

import com.wheel.core.utils.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * <dt>Description:HandlerMethodArgumentResolverRegister</dt>
 * <dd>请求处理注册器</dd>
 *
 * @author wnfbx
 * @version 1.0 <i>2021/6/11 14:51</i>
 * @since jdk1.7+
 */
@Deprecated
public class HandlerMethodArgumentResolverRegister {

    private static final Logger log = LoggerFactory.getLogger(HandlerMethodArgumentResolverRegister.class);

    /**
     * <dt>Description:mvcConfigurationRegister</dt>
     * <dd>
     * RequestMappingHandlerAdapter.getDefaultArgumentResolvers() 中查看替换对象
     * </dd>
     * <dd>author: wnfbx</dd>
     * <dd>version:1.0 <i>2021/6/11 15:07</i></dd>
     *
     * @param target
     * @param processor
     * @param adapter
     * @return T
     * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter private getDefaultArgumentResolvers()
     * @since jdk1.7+
     */
    public static <T extends HandlerMethodArgumentResolver> T mvcConfigurationRegister(Class<? extends HandlerMethodArgumentResolver> target, T processor, RequestMappingHandlerAdapter adapter) {
        //不能修改 重置
        List<HandlerMethodArgumentResolver> argumentResolvers = adapter.getArgumentResolvers();
        ArrayList<HandlerMethodArgumentResolver> newArgumentResolver = new ArrayList<>();
        boolean replaced = false;
        for (HandlerMethodArgumentResolver argumentResolver : argumentResolvers) {
            if (target.isInstance(argumentResolver)) {
                replaced = true;
                Log.info(log, "自定义参数处理器{}代替系统默认处理器{}", processor.getClass().getName(), target.getName());
                newArgumentResolver.add(processor);
            } else {
                newArgumentResolver.add(argumentResolver);
            }
        }
        if (!replaced) {
            Log.info(log, "不可能 自定义参数处理器{} 没能替代默认处理器{}", processor.getClass().getName(), target.getName());
        }
        //重置
        adapter.setArgumentResolvers(newArgumentResolver);
        return processor;
    }
}
